##NSSO Energy Sources Reports Data Table
library(dplyr)
library(tidyr)
library(ggplot2)
library(forcats)

##Data taken from the following NSSO reports
#Energy Sources of Indian Households for Cooking and Lighting, 2004-05 (Report No. 511(61/1.0/4))
#Energy Sources of Indian Households for Cooking and Lighting, 2009-10 (Report No. 542 (66/1.0/4))
#Energy Sources of Indian Households for Cooking and Lighting, 2011-12 (Report No. 567 (68/1.0/4))

nsso_allindia <- data.frame(stringsAsFactors=FALSE,
                         caste = c("ST", "SC", "OBC", "Others", "ST", "SC", "OBC", "Others",
                                   "ST", "SC", "OBC", "Others", "ST", "SC",
                                   "OBC", "Others", "ST", "SC", "OBC", "Others",
                                   "ST", "SC", "OBC", "Others"),
                        survey = c("2004/5", "2004/5", "2004/5", "2004/5", "2004/5", "2004/5",
                                   "2004/5", "2004/5", "2009/10", "2009/10",
                                   "2009/10", "2009/10", "2009/10", "2009/10",
                                   "2009/10", "2009/10", "2011/12", "2011/12",
                                   "2011/12", "2011/12", "2011/12", "2011/12",
                                   "2011/12", "2011/12"),
                    ruralurban = c("rural", "rural", "rural", "rural", "urban", "urban",
                                   "urban", "urban", "rural", "rural", "rural",
                                   "rural", "urban", "urban", "urban", "urban",
                                   "rural", "rural", "rural", "rural", "urban",
                                   "urban", "urban", "urban"),
                           lpg = c(27, 37, 80, 160, 411, 365, 508, 697, 43, 62, 117, 193, 524,
                                   484, 602, 743, 53, 59, 160, 233, 516, 568,
                                   660, 762),
                          elec = c(427, 473, 563, 643, 838, 848, 914, 961, 589, 583, 675, 726,
                                   892, 896, 935, 963, 711, 668, 721, 801, 945,
                                   939, 954, 978)
                 )


nsso_states <- data.frame(stringsAsFactors=FALSE,
                caste = c("ST", "SC", "OBC", "Others", "All", "ST", "SC", "OBC",
                          "Others", "All", "ST", "SC", "OBC", "Others", "All",
                          "ST", "SC", "OBC", "Others", "All", "ST", "SC", "OBC",
                          "Others", "All", "ST", "SC", "OBC", "Others", "All",
                          "ST", "SC", "OBC", "Others", "All", "ST", "SC", "OBC",
                          "Others", "All", "ST", "SC", "OBC", "Others", "All",
                          "ST", "SC", "OBC", "Others", "All", "ST", "SC", "OBC",
                          "Others", "All", "ST", "SC", "OBC", "Others", "All", "ST",
                          "SC", "OBC", "Others", "All", "ST", "SC", "OBC",
                          "Others", "All", "ST", "SC", "OBC", "Others", "All", "ST",
                          "SC", "OBC", "Others", "All", "ST", "SC", "OBC",
                          "Others", "All", "ST", "SC", "OBC", "Others", "All", "ST",
                          "SC", "OBC", "Others", "All", "ST", "SC", "OBC", "Others",
                          "All", "ST", "SC", "OBC", "Others", "All", "ST", "SC",
                          "OBC", "Others", "All", "ST", "SC", "OBC", "Others",
                          "All", "ST", "SC", "OBC", "Others", "All", "ST", "SC",
                          "OBC", "Others", "All", "ST", "SC", "OBC", "Others",
                          "All", "ST", "SC", "OBC", "Others", "All", "ST", "SC",
                          "OBC", "Others", "All", "ST", "SC", "OBC", "Others", "All",
                          "ST", "SC", "OBC", "Others", "All", "ST", "SC", "OBC",
                          "Others", "All", "ST", "SC", "OBC", "Others", "All",
                          "ST", "SC", "OBC", "Others", "All", "ST", "SC", "OBC",
                          "Others", "All", "ST", "SC", "OBC", "Others", "All",
                          "ST", "SC", "OBC", "Others", "All"),
               survey = c("2004/5", "2004/5", "2004/5", "2004/5", "2004/5", "2004/5",
                          "2004/5", "2004/5", "2004/5", "2004/5", "2004/5",
                          "2004/5", "2004/5", "2004/5", "2004/5", "2004/5",
                          "2004/5", "2004/5", "2004/5", "2004/5", "2004/5", "2004/5",
                          "2004/5", "2004/5", "2004/5", "2004/5", "2004/5",
                          "2004/5", "2004/5", "2004/5", "2004/5", "2004/5", "2004/5",
                          "2004/5", "2004/5", "2004/5", "2004/5", "2004/5",
                          "2004/5", "2004/5", "2004/5", "2004/5", "2004/5", "2004/5",
                          "2004/5", "2004/5", "2004/5", "2004/5", "2004/5",
                          "2004/5", "2004/5", "2004/5", "2004/5", "2004/5", "2004/5",
                          "2004/5", "2004/5", "2004/5", "2004/5", "2004/5",
                          "2009/10", "2009/10", "2009/10", "2009/10", "2009/10",
                          "2009/10", "2009/10", "2009/10", "2009/10", "2009/10",
                          "2009/10", "2009/10", "2009/10", "2009/10", "2009/10",
                          "2009/10", "2009/10", "2009/10", "2009/10", "2009/10",
                          "2009/10", "2009/10", "2009/10", "2009/10", "2009/10",
                          "2009/10", "2009/10", "2009/10", "2009/10", "2009/10",
                          "2009/10", "2009/10", "2009/10", "2009/10", "2009/10",
                          "2009/10", "2009/10", "2009/10", "2009/10", "2009/10",
                          "2009/10", "2009/10", "2009/10", "2009/10", "2009/10",
                          "2009/10", "2009/10", "2009/10", "2009/10", "2009/10",
                          "2009/10", "2009/10", "2009/10", "2009/10", "2009/10",
                          "2009/10", "2009/10", "2009/10", "2009/10", "2009/10",
                          "2011/12", "2011/12", "2011/12", "2011/12", "2011/12",
                          "2011/12", "2011/12", "2011/12", "2011/12", "2011/12",
                          "2011/12", "2011/12", "2011/12", "2011/12", "2011/12",
                          "2011/12", "2011/12", "2011/12", "2011/12", "2011/12",
                          "2011/12", "2011/12", "2011/12", "2011/12", "2011/12",
                          "2011/12", "2011/12", "2011/12", "2011/12", "2011/12",
                          "2011/12", "2011/12", "2011/12", "2011/12", "2011/12",
                          "2011/12", "2011/12", "2011/12", "2011/12", "2011/12",
                          "2011/12", "2011/12", "2011/12", "2011/12", "2011/12",
                          "2011/12", "2011/12", "2011/12", "2011/12", "2011/12",
                          "2011/12", "2011/12", "2011/12", "2011/12", "2011/12",
                          "2011/12", "2011/12", "2011/12", "2011/12", "2011/12"),
           ruralurban = c("rural", "rural", "rural", "rural", "rural", "rural",
                          "rural", "rural", "rural", "rural", "rural", "rural",
                          "rural", "rural", "rural", "rural", "rural", "rural",
                          "rural", "rural", "rural", "rural", "rural", "rural",
                          "rural", "rural", "rural", "rural", "rural", "rural",
                          "urban", "urban", "urban", "urban", "urban", "urban",
                          "urban", "urban", "urban", "urban", "urban", "urban",
                          "urban", "urban", "urban", "urban", "urban", "urban",
                          "urban", "urban", "urban", "urban", "urban", "urban",
                          "urban", "urban", "urban", "urban", "urban", "urban",
                          "rural", "rural", "rural", "rural", "rural", "rural", "rural",
                          "rural", "rural", "rural", "rural", "rural", "rural",
                          "rural", "rural", "rural", "rural", "rural", "rural",
                          "rural", "rural", "rural", "rural", "rural", "rural",
                          "rural", "rural", "rural", "rural", "rural", "urban",
                          "urban", "urban", "urban", "urban", "urban", "urban",
                          "urban", "urban", "urban", "urban", "urban", "urban",
                          "urban", "urban", "urban", "urban", "urban", "urban",
                          "urban", "urban", "urban", "urban", "urban", "urban",
                          "urban", "urban", "urban", "urban", "urban", "rural", "rural",
                          "rural", "rural", "rural", "rural", "rural", "rural",
                          "rural", "rural", "rural", "rural", "rural", "rural",
                          "rural", "rural", "rural", "rural", "rural", "rural",
                          "rural", "rural", "rural", "rural", "rural", "rural",
                          "rural", "rural", "rural", "rural", "urban", "urban",
                          "urban", "urban", "urban", "urban", "urban", "urban",
                          "urban", "urban", "urban", "urban", "urban", "urban",
                          "urban", "urban", "urban", "urban", "urban", "urban",
                          "urban", "urban", "urban", "urban", "urban", "urban", "urban",
                          "urban", "urban", "urban"),
                state = c("bihar", "bihar", "bihar", "bihar", "bihar", "jharkhand",
                          "jharkhand", "jharkhand", "jharkhand", "jharkhand",
                          "madhyapradesh", "madhyapradesh", "madhyapradesh",
                          "madhyapradesh", "madhyapradesh", "odisha", "odisha",
                          "odisha", "odisha", "odisha", "uttarpradesh", "uttarpradesh",
                          "uttarpradesh", "uttarpradesh", "uttarpradesh",
                          "westbengal", "westbengal", "westbengal", "westbengal",
                          "westbengal", "bihar", "bihar", "bihar", "bihar", "bihar",
                          "jharkhand", "jharkhand", "jharkhand", "jharkhand",
                          "jharkhand", "madhyapradesh", "madhyapradesh",
                          "madhyapradesh", "madhyapradesh", "madhyapradesh", "odisha",
                          "odisha", "odisha", "odisha", "odisha", "uttarpradesh",
                          "uttarpradesh", "uttarpradesh", "uttarpradesh",
                          "uttarpradesh", "westbengal", "westbengal", "westbengal",
                          "westbengal", "westbengal", "bihar", "bihar", "bihar", "bihar",
                          "bihar", "jharkhand", "jharkhand", "jharkhand",
                          "jharkhand", "jharkhand", "madhyapradesh", "madhyapradesh",
                          "madhyapradesh", "madhyapradesh", "madhyapradesh",
                          "odisha", "odisha", "odisha", "odisha", "odisha",
                          "uttarpradesh", "uttarpradesh", "uttarpradesh", "uttarpradesh",
                          "uttarpradesh", "westbengal", "westbengal", "westbengal",
                          "westbengal", "westbengal", "bihar", "bihar", "bihar",
                          "bihar", "bihar", "jharkhand", "jharkhand",
                          "jharkhand", "jharkhand", "jharkhand", "madhyapradesh",
                          "madhyapradesh", "madhyapradesh", "madhyapradesh",
                          "madhyapradesh", "odisha", "odisha", "odisha", "odisha", "odisha",
                          "uttarpradesh", "uttarpradesh", "uttarpradesh",
                          "uttarpradesh", "uttarpradesh", "westbengal", "westbengal",
                          "westbengal", "westbengal", "westbengal", "bihar",
                          "bihar", "bihar", "bihar", "bihar", "jharkhand", "jharkhand",
                          "jharkhand", "jharkhand", "jharkhand", "madhyapradesh",
                          "madhyapradesh", "madhyapradesh", "madhyapradesh",
                          "madhyapradesh", "odisha", "odisha", "odisha", "odisha",
                          "odisha", "uttarpradesh", "uttarpradesh",
                          "uttarpradesh", "uttarpradesh", "uttarpradesh", "westbengal",
                          "westbengal", "westbengal", "westbengal", "westbengal",
                          "bihar", "bihar", "bihar", "bihar", "bihar", "jharkhand",
                          "jharkhand", "jharkhand", "jharkhand", "jharkhand",
                          "madhyapradesh", "madhyapradesh", "madhyapradesh",
                          "madhyapradesh", "madhyapradesh", "odisha", "odisha", "odisha",
                          "odisha", "odisha", "uttarpradesh", "uttarpradesh",
                          "uttarpradesh", "uttarpradesh", "uttarpradesh",
                          "westbengal", "westbengal", "westbengal", "westbengal",
                          "westbengal"),
                  lpg = c(0L, 6L, 13L, 52L, 17L, 2L, 10L, 16L, 42L, 14L, 14L, 7L, 42L,
                          112L, 38L, 2L, 9L, 27L, 106L, 29L, 13L, 15L, 35L,
                          129L, 48L, 28L, 23L, 51L, 54L, 43L, 324L, 428L, 483L,
                          690L, 539L, 113L, 141L, 314L, 723L, 427L, 448L, 317L, 391L,
                          766L, 545L, 165L, 110L, 254L, 563L, 358L, 351L, 379L,
                          486L, 699L, 561L, 381L, 249L, 404L, 520L, 461L, 1L, 7L,
                          34L, 74L, 35L, 20L, 18L, 24L, 73L, 25L, 14L, 34L, 48L,
                          208L, 55L, 3L, 20L, 39L, 96L, 37L, 7L, 29L, 43L, 146L,
                          58L, 30L, 23L, 29L, 66L, 48L, 592L, 336L, 400L, 772L,
                          502L, 468L, 432L, 542L, 828L, 623L, 423L, 411L, 536L,
                          771L, 602L, 123L, 218L, 415L, 555L, 414L, 872L, 499L,
                          537L, 742L, 619L, 583L, 312L, 389L, 640L, 564L, 14L, 16L,
                          42L, 163L, 59L, 22L, 16L, 33L, 56L, 29L, 22L, 42L,
                          59L, 182L, 62L, 12L, 8L, 36L, 129L, 39L, 96L, 23L, 64L,
                          146L, 67L, 64L, 40L, 67L, 83L, 66L, 587L, 493L, 561L,
                          785L, 605L, 365L, 222L, 473L, 808L, 539L, 509L, 434L,
                          593L, 858L, 652L, 189L, 113L, 395L, 687L, 435L, 759L, 500L,
                          573L, 839L, 668L, 584L, 393L, 581L, 601L, 565L),
                 elec = c(2L, 49L, 111L, 145L, 101L, 127L, 213L, 325L, 455L, 260L,
                          595L, 667L, 716L, 830L, 692L, 77L, 204L, 430L, 583L,
                          315L, 120L, 151L, 233L, 386L, 240L, 296L, 268L, 435L,
                          377L, 342L, 569L, 510L, 740L, 835L, 738L, 760L, 839L,
                          836L, 952L, 871L, 965L, 909L, 951L, 992L, 964L, 539L,
                          650L, 840L, 914L, 813L, 582L, 696L, 805L, 934L, 844L,
                          791L, 745L, 906L, 905L, 873L, 27L, 176L, 153L, 311L, 183L,
                          279L, 452L, 556L, 701L, 455L, 636L, 720L, 762L, 887L,
                          734L, 270L, 440L, 630L, 811L, 538L, 344L, 242L, 327L,
                          481L, 332L, 276L, 354L, 547L, 531L, 459L, 784L, 524L,
                          645L, 865L, 696L, 844L, 961L, 900L, 936L, 910L, 994L,
                          961L, 961L, 993L, 975L, 612L, 697L, 877L, 970L, 859L, 881L,
                          720L, 782L, 849L, 802L, 702L, 869L, 945L, 945L, 926L,
                          192L, 160L, 247L, 403L, 258L, 509L, 564L, 718L, 668L,
                          621L, 817L, 822L, 853L, 914L, 845L, 459L, 701L, 728L,
                          853L, 676L, 516L, 323L, 393L, 564L, 404L, 685L, 618L,
                          782L, 744L, 702L, 518L, 735L, 811L, 877L, 812L, 948L,
                          914L, 978L, 967L, 964L, 976L, 973L, 971L, 995L, 980L, 839L,
                          950L, 970L, 988L, 962L, 869L, 821L, 844L, 947L, 881L,
                          877L, 884L, 980L, 956L, 945L)
        )


#prepare data for visualisation
nsso_allindia_vis <-
  nsso_allindia %>% filter(caste != "All") %>% gather(service, share, -c(caste, survey, ruralurban)) %>%
  mutate(service = fct_recode(factor(service), 
                              "Electricity" = "elec",
                              "LPG" = "lpg"),
         caste = fct_recode(factor(caste, levels = c("SC","ST","OBC","Others")),
                            "Scheduled Caste" = "SC",
                            "Scheduled Tribe" = "ST",
                            "Other Backward Castes" = "OBC"),
         ruralurban = fct_recode(factor(ruralurban),
                            "Rural" = "rural",
                            "Urban" = "urban"),
         share = share / 1000)

nsso_states_vis <-
  nsso_states %>% filter(ruralurban == "rural", caste != "All") %>% gather(service, share, -c(caste, survey, ruralurban, state)) %>%
  mutate(service = fct_recode(factor(service), 
                                        "Electricity" = "elec",
                                        "LPG" = "lpg"),
         caste = fct_recode(factor(caste, levels = c("SC","ST","OBC","Others")),
                           "Scheduled Caste" = "SC",
                           "Scheduled Tribe" = "ST",
                           "Other Backward Castes" = "OBC"),
         state = fct_recode(factor(state),
                            "Bihar" = "bihar",
                            "Jharkhand" = "jharkhand",
                            "Madhya Pradesh" = "madhyapradesh",
                            "Odisha" = "odisha",
                            "Uttar Pradesh" = "uttarpradesh",
                            "West Bengal" = "westbengal"),
         share = share / 1000)
  
# for (i in unique(nsso$state)) {
#   assign(paste0("State_",i), nsso_vis %>% filter(state == i) %>% ggplot(aes(x = survey,share, colour = caste, group = caste)) +
#     geom_point(size = 1.5) +
#     geom_line(size = 1) +
#     facet_grid(service ~ ruralurban) +
#     scale_y_continuous(labels = scales::percent,
#                        breaks = seq(0, 1, by = 0.2)) +
#     coord_cartesian(ylim = c(0, 1)) +
#     labs(x = "Survey Year", y = "Share of population with access", 
#          
#          colour = "Caste Group",
#          title = paste0("Historical household energy access trends in ",i),
#          subtitle = "Nationally representative survey data generated by the NSSO",
#          caption = ("Note: Data taken from the following NSSO reports; 511(61/1.0/4), 542(66/1.0/4), 567(68/1.0/4)")) +
#     theme_bw() +
#     theme(strip.background = element_rect(fill = NA, color = "black"),
#           plot.caption = element_text(hjust = 1.5)))
# }

nsso_allindia_vis %>% ggplot(aes(x = survey,share, colour = caste, group = caste)) +
  geom_point(size = 1.5) +
  geom_line(size = 1) +
  facet_grid(service ~ ruralurban) +
  scale_y_continuous(labels = scales::percent,
                     breaks = seq(0, 1, by = 0.2)) +
  coord_cartesian(ylim = c(0, 1)) +
  labs(x = "Survey Year", y = "Share of population with access", 
       colour = "Social Group") +
  theme_bw() +
  theme(strip.background = element_rect(fill = NA, color = "black"),
        plot.caption = element_text(hjust = 1))

ggsave(here::here("Manuscript", "Figures", "nsso_access_allindia.png"), 
       device = "png", width = 8, height = 8*0.68, units = "in")

nsso_states_vis %>% ggplot(aes(x = survey,share, colour = caste, group = caste)) +
  geom_point(size = 1.5) +
  geom_line(size = 1) +
  facet_grid(service ~ state) +
  scale_y_continuous(labels = scales::percent,
                     breaks = seq(0, 1, by = 0.2)) +
  coord_cartesian(ylim = c(0, 1)) +
  labs(x = "Survey Year", y = "Share of rural population with access", 
       
       colour = "Social Group") +
  guides(colour = guide_legend(reverse=T)) +
  theme_bw() +
  theme(strip.background = element_rect(fill = NA, color = "black"),
        plot.caption = element_text(hjust = 1))

ggsave(here::here("Manuscript", "Figures", "nsso_access_states.png"), 
       device = "png", width = 14, height = 6, units = "in")

  